<?php

/*
 * +----------------------------------------------------------------------
 * 作 者：huk
 * +----------------------------------------------------------------------
 * 创建日期：2016-08-27
 * +----------------------------------------------------------------------
 * 文件描述：测验内容>>附件题
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class ComSelectAction extends CommonAction
{

    public function get_item_edit($formid)
    {
        // 查询数据
        $model = new ComSelectModel();
        $data = $model->field("id,
							   title,
							   code,
							   score,
							   type,
							   operator,
							   update_date")
            ->where("id = '" . $formid . "'")
            ->find();
        $data['type_name'] = $this->get_field_dict_value('com_select', 'type', $data['type']);
        return $data;
    }

    public function get_item_detail($roomid, $item_id, $userid)
    {
        // 查询数据
    }

    /**
     * 题目添加页面
     */
    public function add()
    {
        // 当前所剩分值
        $formid = $this->_request('formid');
        
        $this->assign('item_info', $this->get_form_info($formid)); // 表单基本信息（头部显示）
        $this->assign('type_list', set_field_dict_list('com_select', 'type'));
        $this->display();
    }

    /**
     * 将题目添加至数据库
     */
    public function insert()
    {
        $post_data = $this->_request();
        $formid = $this->_request('formid');
        $mode = $this->_request('mode');
        $model = new ComSelectModel();
        $data = $model->create();
        if (false !== $data) {
            $id = $model->add();
            $data = R('Admin/QuestionTestEdit/add_item_info', array(
                $formid,
                'comSelect',
                $id
            ));
            if (false !== $data) {
                $this->success('选择器添加成功！', __APP__ . '?g=Admin&m=QuestionTestEdit&a=index&formid=' . $formid . '&mode=' . $mode . '#comSelect_' . $id);
            } else {
                $this->error('选择器添加失败');
            }
        } else {
            $this->error('添加失败' . $model->getError());
        }
    }

    /**
     * 题目修改页面
     */
    public function edit()
    {
        // 查询当前选择题信息
        $roomid = $this->_request('roomid');
        $model = new ComSelectModel();
        $data = $model->where("id = '" . $_REQUEST['questionid'] . "'")->find();
        $this->assign('type_list', set_field_dict_list('com_select', 'type'));
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 将题目信息更新至数据库
     */
    public function update()
    {
        $post_data = $this->_request();
        dump($post_data);
        $questionid = $this->_request('questionid');
        $formid = $this->_request('formid');
        $mode = $this->_request('mode');
        $model = new ComSelectModel();
        if (false !== $model->create($post_data, 1)) {
            if (false !== $model->where("id = '" . $questionid . "'")->save($post_data)) {
                $this->success('更新成功！', __APP__ . '?g=Admin&m=QuestionTestEdit&a=index&formid=' . $formid . '&mode=' . $mode . '#comSelect_' . $questionid);
            } else {
                $this->error('更新失败！!!!!!');
            }
        } else {
            $this->error('更新失败！' . $model->getError());
        }
    }

    /**
     * 删除题目信息
     */
    public function delete()
    {
        $model = new ComSelectModel();
        $model->startTrans(); // 启动事务
        $id = $this->_request('questionid');
        $formid = $this->_request('formid');
        if (! empty($id)) {
            if (false !== $model->where("id = '" . $id . "'")->delete()) {
                $result = R('Admin/QuestionTestEdit/delete_item_info', array(
                    $formid,
                    'comSelect',
                    $id
                ));
                if ($result) {
                    $model->commit(); // 提交事务
                    $this->ajaxReturn(null, '删除成功！', 1);
                } else {
                    $model->rollback(); // 回滚事务
                    $this->ajaxReturn(null, '删除失败！', 0);
                }
            } else {
                $model->rollback(); // 回滚事务
                $this->ajaxReturn(null, '删除失败！', 0);
            }
        } else {
            $this->ajaxReturn(null, '未找到要删除的题目！', 0);
        }
    }
}
?>